package com.quotes.ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import org.apache.log4j.Logger;
import com.quotes.Parameters;
import com.quotes.charts.AdxChart;
import com.quotes.charts.AtrChart;
import com.quotes.charts.BuySellChart;
import com.quotes.charts.CandleStickChart;
import com.quotes.charts.CoppockChart;
import com.quotes.charts.MacdChart;
import com.quotes.charts.MomentumChart;
import com.quotes.charts.RocChart;
import com.quotes.charts.RsiChart;
import com.quotes.datamodell.Quotes;
import com.quotes.dates.TimeRange;

public class ChartsPanel extends JPanel implements ActionListener
{
	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(getClass().toString());
	private JPanel mainPanel = null;
	private GridLayout layout = null;
	private TreeMap<String, JPanel> charts = new TreeMap<String, JPanel>();
	private Quotes quotes = null;
	private TimeRange range = null;
	private Parameters parameters = null;
	private JPanel chartsSelectPanel = null;

	public ChartsPanel(TimeRange range, Parameters parameters) {
		this.range = range;
		this.parameters = parameters;
		
		this.setLayout(new BorderLayout());

		layout = new GridLayout(0, 1);
		mainPanel = new JPanel(layout);
		add(mainPanel, BorderLayout.CENTER);
		
		chartsSelectPanel = new JPanel(new GridLayout(20, 1));
		add(chartsSelectPanel, BorderLayout.EAST);

		addCheckBox(chartsSelectPanel, "CandleStick", "candleStick");
		addCheckBox(chartsSelectPanel, "Macd.12-26-9", "macd.12-26-9");
		addCheckBox(chartsSelectPanel, "Macd.13-39-9", "macd.13-39-9");
		addCheckBox(chartsSelectPanel, "RSI.14", "rsi.14");
		addCheckBox(chartsSelectPanel, "ADX.14", "adx.14");
		addCheckBox(chartsSelectPanel, "ATR.14", "atr.14");
		addCheckBox(chartsSelectPanel, "Momentum.25", "momentum.25");
		addCheckBox(chartsSelectPanel, "ROC.25", "roc.25");
		addCheckBox(chartsSelectPanel, "Coppock.14-11-10", "coppock.14-11-10");
		addCheckBox(chartsSelectPanel, "BuySell", "buysell");
	}

	private void addCheckBox(JPanel chartsSelectPanel, String name, String actionCommand) {
		JCheckBox checkBox = new JCheckBox(name);
		checkBox.setActionCommand(actionCommand);
		checkBox.addActionListener(this);
		chartsSelectPanel.add(checkBox);
	}
	
	private void increaseRows() {
		layout.setRows(layout.getRows() + 1);
	}
	
	private void decreaseRows() {
		layout.setRows(layout.getRows() - 1);
	};
	
	private JPanel getChartByName(String name) {
		if (quotes != null) {
			if (name == "candleStick") {
				return new CandleStickChart(quotes, range);
			} else if (name == "macd.12-26-9") {
				return new MacdChart(quotes, range, 12, 26, 9);
			} else if (name == "macd.13-39-9") {
				return new MacdChart(quotes, range, 13, 39, 9);
			} else if (name == "rsi.14") {
				return new RsiChart(quotes, range, 14);
			} else if (name == "adx.14") {
				return new AdxChart(quotes, range, 14);
			} else if (name == "atr.14") {
				return new AtrChart(quotes, range, 14);
			} else if (name == "momentum.25") {
				return new MomentumChart(quotes, range, 25);
			} else if (name == "roc.25") {
				return new RocChart(quotes, range, 25);
			} else if (name == "coppock.14-11-10") {
				return new CoppockChart(quotes, range, 14, 11, 10);
			} else if (name == "buysell") {
				return new BuySellChart(quotes, range, parameters);
			}
		}
		
		return null;
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		if (quotes != null) {
			JCheckBox checkBox = (JCheckBox)event.getSource();
			if (checkBox.isSelected()) {
				increaseRows();
				JPanel chart = getChartByName(event.getActionCommand());
				charts.put(event.getActionCommand(), chart);
				mainPanel.add(chart);
			} else {
				JPanel chart = charts.get(event.getActionCommand());
				mainPanel.remove(chart);
				charts.remove(event.getActionCommand());
				decreaseRows();
			}
			
			revalidate();
		}
	}
	
	private List<String> getSelectedChartsNames() {
		List<String> chartsNames = new ArrayList<String>();
		
		for (Component component : chartsSelectPanel.getComponents()) {
			if (component instanceof JCheckBox) {
				JCheckBox checkBox = (JCheckBox)component;
				if (checkBox.isSelected()) {
					chartsNames.add(checkBox.getActionCommand());
				}
			}
		}
		
		return chartsNames;
	}
	
	private void repaintAllCharts() {
		mainPanel.removeAll();
		charts.clear();
		
		for (String chartsName : getSelectedChartsNames()) {
			JPanel chart = getChartByName(chartsName);
			charts.put(chartsName, chart);
			mainPanel.add(chart);
		}
		
		revalidate();
	}
	
	public void setQuotes(Quotes quotes) {
		this.quotes = quotes;
		this.repaintAllCharts();
	}
	
	public void setTimeRange(TimeRange range) {
		this.range = range;
		repaintAllCharts();
	}

	public void setParameters(Parameters parameters) {
		this.parameters = parameters;
		repaintAllCharts();
	}
}
